<?php 

class BlogModel extends Model{
	public $table='blog_blog';
	public $error;
	public function getAll()
	{
		$result=$this->query("select count(*) as c from blog_blog");
		$page=new Page($result[0]['c'],5);
		$pagestr=$page->show();//获得页码
		$limit=$page->limit();//limit 0,2
		$sql="select * from blog_blog b join blog_category c on c.cid=b.cid order by bid desc limit $limit";
		$data=$this->query($sql);
		return array('data'=>$data,'page'=>$pagestr);
	}
	//统计博客数量
	public function count()
	{
		$sql="select count(*) as c from blog_blog";
		$data=$this->query($sql);
		return $data[0]['c'];
	}
	//统计最大浏览数
	public function maxlook()
	{
		$sql="select click as c from blog_blog order by click desc";
		$data=$this->query($sql);
		return $data[0]['c'];
	}

	//添加博客
	public function addBlog()
	{
		//有缩略图时上传处理
		if (!empty($_FILES['thumb']['name'])) {
			$upload=new Upload;
			$files=$upload->upload();
			//存储图片路径
			$_POST['thumb']=$files[0]['path'];
		}else{
			$_POST['thumb']="Upload/moren.jpg";
		}
		//发表时间
		$_POST['addtime']=time();
		return $this->insert($_POST);
	}
	//修改博客
	public function editBlog()
	{
		//有缩略图上传时处理上传
		if (!empty($_FILES['thumb']['name'])) {
			//删除原来的图片
			if (is_file($_POST['hd_thumb'])) {
				echo "string";
				unlink($_POST['hd_thumb']);
			}
			$upload=new Upload;
			$files=$upload->upload();
			$_POST['thumb']=$files[0]['path'];
		}
		//发表时间
		$_POST['addtime']=time();
		unset($_POST['hd_thumb']);
		return $this->where("bid={$_POST['bid']}")->update($_POST);
	}

	//删除博客
	public function delBlog($data)
	{
		if (!empty($data)) { 
			$file=$this->where(" bid=$data ")->find();
			if (isset($file[0]['thumb'])) {
				unlink($file[0]['thumb']);
			}
			$this->where(" bid=$data ")->delete();
			return true;
		}else{
			$this->error="删除失败";
			return false;
		}
	}


}



 ?>